#include "main.h"
extern "C"{
#undef main
void event_ddd(void *data){
    SDL_Event  *event = (SDL_Event *)data;
    static int speed =dif_speed,shift_speed=0;
    static int move =0;
    if (SDL_PollEvent(event))
    {
        if(event->type==SDL_QUIT){
            //这里考虑加所有资源释放
            Game_DeInit();
            exit(0);
        }else if(event->type==SDL_KEYDOWN){
//            switch (event->key.keysym.sym)
//            {  // 按键移动纹理坐标位置
//                case SDLK_w: move=1; break;
//                case SDLK_s: move=2; break;
//                case SDLK_a: move=3; break;
//                case SDLK_d: move=4; break;
//                case SDLK_w: My_Crasg.y+=10; break;
//                case SDLK_s: My_Crasg.y-=10; break;
//                case SDLK_a: My_Crasg.x+=10; break;
//                case SDLK_d: My_Crasg.x-=10; break;
//
//            }
            if (event->key.keysym.mod & (KMOD_SHIFT | KMOD_LSHIFT)) {
                // Shift键被按下时的处理逻辑
                shift_speed=dif_shift_speed;
          //      printf("event come! SDL_KEYDOWN KMOD_SHIFT \n");
            }else{
                shift_speed=0;
           //     printf("event come! SDL_KEYUP KMOD_SHIFT \n");
            }
        }else if(event->type==SDL_KEYUP){
//            switch (event->key.keysym.sym)
//            {  // 按键移动纹理坐标位置
//                case SDLK_w: move=0; break;
//                case SDLK_s: move=0; break;
//                case SDLK_a: move=0; break;
//                case SDLK_d: move=0; break;
//
//            }
           // printf("%d %d\n",lead.Rect.x,lead.Rect.y);
            if (event->key.keysym.mod & (KMOD_SHIFT | KMOD_LSHIFT)) {
                // Shift键被按下时的处理逻辑
                shift_speed=2;
           //     printf("event come! SDL_KEYDOWN KMOD_SHIFT \n");
            }else{
                shift_speed=0;
          //      printf("event come! SDL_KEYUP KMOD_SHIFT \n");
            }

        }
        else{


        }
      //  printf("event come! event->type =%d \n",event->type);
    }


    const Uint8 *state = SDL_GetKeyboardState(nullptr);
 //   printf("event come! event->type                        %d                %d \n",speed,shift_speed);
    if ((!state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])) {
        // 'A'键被按下，执行相应操作
        speed_Y =0;speed_X=0;
    }else if((state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])){//A按下其余都没按
        speed_X = -(speed+shift_speed);speed_Y =0;
    } else if((!state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])){// S
        speed_Y = (speed+shift_speed);speed_X=0;
    } else if((!state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(state[SDL_SCANCODE_D])){// D
        speed_X = (speed+shift_speed);speed_Y =0;
    }else if((!state[SDL_SCANCODE_A])&&(state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])){//W
        speed_Y = -(speed+shift_speed);speed_X=0;
    }else if((state[SDL_SCANCODE_A])&&(state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])){ //A W
        speed_Y = -(speed+shift_speed);speed_X = -(speed+shift_speed);
    }else if((!state[SDL_SCANCODE_A])&&(state[SDL_SCANCODE_W])&&(!state[SDL_SCANCODE_S])&&(state[SDL_SCANCODE_D])){ //W D
        speed_Y = -(speed+shift_speed);speed_X = (speed+shift_speed);
    }else if((!state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(state[SDL_SCANCODE_S])&&(state[SDL_SCANCODE_D])){ //S D
        speed_Y = (speed+shift_speed);speed_X = (speed+shift_speed);
    }else if((state[SDL_SCANCODE_A])&&(!state[SDL_SCANCODE_W])&&(state[SDL_SCANCODE_S])&&(!state[SDL_SCANCODE_D])){ //A S
        speed_X = -(speed+shift_speed);speed_Y = (speed+shift_speed);
    }


}
int main() {
    Game_Init();

    map_init();
    char mappath[128]="../resources/map/home.json";


    LNode* Mmap_list =(LNode*) map_load(mappath);
    if(Mmap_list== nullptr)
        return -1;
    SDL_Rect ss1={
            .x =0,
            .y =0,
            .w =24,
            .h =24
    };
    Spirit_Rect dada={
            .x=2,
            .y=2,
            .w=13,
            .h=23
    };
    Crash_load(dada,1);
    while (true)
    {
        timeclock=SDL_GetTicks64();
        time_begin=timeclock;
        SDL_RenderClear(ren);
        event_handle(event_ddd,&event);//事件处理

        Mmap_Linkrun(Mmap_list);  //地图加载
        int num=Mspirit_Linkrun(Spirit_Link);  //精灵加载


        PPS_SHOW();
        SDL_RenderPresent(ren); //渲染

        //=========帧率控制===============
        timeclock=1000/gFPS-(SDL_GetTicks64()-timeclock);
        if((timeclock>0)&&(timeclock<=1000/gFPS)){
            SDL_Delay(timeclock);
        }
        SDL_RenderPresent(ren); //渲染
        time_end=SDL_GetTicks64();
        sprintf(fps_num,"FPS:%llu\n",1000/(time_end-time_begin)); //fps计算
       // printf("%d %d \n",My_Crasg.x,My_Crasg.y);
    }


    IMG_Quit();
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(ren);
    SDL_Quit();
}

}

